package com.demo.statemachine.d05.formStateMachine;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineBuilder;

import java.util.EnumSet;

@Configuration
@EnableStateMachine
public class FormStateMachineBuilder {
    @Autowired
    private BeanFactory beanFactory;
    private final static String MACHINEID = "formMachine";

    public StateMachine<FormStates, FormEvents> build() throws Exception {
        StateMachineBuilder.Builder<FormStates, FormEvents> builder = StateMachineBuilder.builder();
        System.out.println("构建Form状态机");
        builder.configureConfiguration()
                .withConfiguration()
                .machineId(MACHINEID)
                .beanFactory(beanFactory);

        builder.configureStates()
                .withStates()
                .initial(FormStates.ZERO)
                .states(EnumSet.allOf(FormStates.class));

        builder.configureTransitions()

                .withExternal()
                .source(FormStates.ZERO).target(FormStates.ONE)
                .event(FormEvents.ONE)

                .and()
                .withExternal()
                .source(FormStates.ONE).target(FormStates.TWO)
                .event(FormEvents.TWO);

        return builder.build();
    }
}
